/**
 * @file bsp_comm.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "hal_adapter.h"
#include "bsp_comm.h"


#include "bsp_uart.h"
#include "global.h"

/**
 * @brief 仪表通讯口初始化
 * @return int32_t 
 */
int32_t bsp_comm_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    
    /* enable the gpio clock */
    comm_gpioclk_enable();

    GPIO_InitStruct.Pin = COMM_UART_TX_PIN|COMM_UART_RX_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = COMM_UART_PORT_AF;
    HAL_GPIO_Init(COMM_UART_PORT, &GPIO_InitStruct);

    /* Peripheral clock enable */
    comm_uartclk_enable();
    
    bsp_uart_init(comm_uart_id);

    return 0;
}


/**
 * @brief  打开 INT 中断
 * @note   固定使用"上升沿"触发中断.
 * @param  isr: [I], 中断服务程序, 请将该ISR注册到 INT1 中断服务程序中;
 *         ptr: [I], 中断服务程序 isr 使用的 ptr.
 * @retval none
 */
void bsp_comm_int_enable(void (*isr)(void *ptr), void *ptr)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
	/* GPIO Ports Clock Enable */
	comm_gpioclk_enable();

	/*Configure GPIO pin : comm_rx_Pin */
	GPIO_InitStruct.Pin = COMM_UART_RX_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(COMM_UART_PORT, &GPIO_InitStruct);

	/* EXTI interrupt init*/
	HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
	HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}

/**
 * @brief  关闭 INT 中断
 */
void bsp_comm_int_disable(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
	
    /* USER CODE BEGIN MX_GPIO_Init_1 */
	/* USER CODE END MX_GPIO_Init_1 */

	/* GPIO Ports Clock Enable */
	comm_gpioclk_enable();

	/*Configure GPIO pin : comm_int_Pin */
	GPIO_InitStruct.Pin = COMM_UART_RX_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(COMM_UART_PORT, &GPIO_InitStruct);

	/* EXTI interrupt init*/
	HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
	HAL_NVIC_DisableIRQ(EXTI1_IRQn);
}


/**
 * @brief comm串口写入数据 
 * @param  data             串口数据发送缓存地址
 * @param  size             串口数据长度
 * @return ssize_t 
 */
ssize_t bsp_comm_write(const void *data, size_t size)
{
    ssize_t wr_cnt = 0;
    
    wr_cnt = bsp_uart_itwrite(comm_uart_id, data, size);
    
    return wr_cnt;
}

/**
 * @brief 串口读取数据
 * @param  data             串口数据接收缓存地址
 * @param  size             最大读取长度
 * @return ssize_t 
 */
ssize_t bsp_comm_read(void *data, size_t size)
{
    ssize_t wr_cnt = 0;
    
    wr_cnt = bsp_uart_read(comm_uart_id, data, size);
    
    return wr_cnt;    
}

/**
 * @brief 设置串口波特率
 * @param  bps              目标串口波特率
 */
void bsp_comm_set_baudrate(uint32_t bps)
{
    bsp_uart_set_baudrate(comm_uart_id, bps);
    return;
}


/**
 * @brief 进入休眠
 * 
 * @return int32_t 
 */
int32_t bsp_comm_uart_sleep(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = COMM_UART_TX_PIN|COMM_UART_RX_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = COMM_UART_PORT_AF;
    HAL_GPIO_Init(COMM_UART_PORT, &GPIO_InitStruct);

    /* 设置RX中断(与Modem RI 管脚中断号冲突，但可用) */
    bsp_comm_int_enable(NULL, NULL); 

    return 0;
}

/**
 * @brief 使能串口唤醒
 * 
 */
void bsp_comm_uart_wakeup(void)
{
    /* 禁用中断 */
    bsp_comm_int_disable();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = COMM_UART_TX_PIN|COMM_UART_RX_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = COMM_UART_PORT_AF;
    HAL_GPIO_Init(COMM_UART_PORT, &GPIO_InitStruct);
}

